package com.example.gzysmartcity.ui.fragment.me;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SPUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentThemeBinding;
import com.example.gzysmartcity.repository.Theme;
import com.example.gzysmartcity.ui.base.BaseFragment;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ThemeFragment extends BaseFragment<FragmentThemeBinding> implements View.OnClickListener {
    private List<String> mStringTheme;

    @Override
    protected FragmentThemeBinding viewBinding() {
        return FragmentThemeBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }


    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mStringTheme = new ArrayList<>(Arrays.asList("明亮", "黄色", "绿色", "蓝色", "系统深色模式", "跟随系统夜间模式"));
//        SPUtils.getInstance("theme", Context.MODE_PRIVATE).put("themeName", "明亮");
        b.radioYellow.setOnClickListener(this);
        b.radioBlue.setOnClickListener(this);
        b.radioGreen.setOnClickListener(this);
        b.radioLight.setOnClickListener(this);
        b.checkSysNight.setOnClickListener(this);
        b.checkSysblack.setOnClickListener(this);
        switch (SPUtils.getInstance("theme", Context.MODE_PRIVATE).getInt("theme", 1)) {
            case 1:
                b.radioLight.setChecked(true);
                break;
            case 2:
                b.radioYellow.setChecked(true);
                break;
            case 3:
                b.radioGreen.setChecked(true);
                break;
            case 4:
                b.radioBlue.setChecked(true);
                break;
            case 5:
                b.checkSysblack.setChecked(true);
                isBlack(true);
                break;
            case 6:
                b.checkSysNight.setChecked(true);
                break;
        }

//        tag = new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                Log.i("TAG", "setOnCheckedChangeListener: ");
//                switch (checkedId) {
//                    case R.id.radio_light:
//                        break;
//                    case R.id.radio_yellow:
//                        requireActivity().recreate();
//                        b.radioTheme.setOnCheckedChangeListener(tag);
//                        break;
//                    case R.id.radio_green:
//                        break;
//                    case R.id.radio_blue:
//                        break;
//                }
//            }
//        };

    }


    @Override
    protected String setTitle() {
        return "主题";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return null;
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        Theme theme = Theme.LIGHT;
        switch (v.getId()) {
            case R.id.radio_blue:
                theme = Theme.BLUE;
                break;
            case R.id.radio_yellow:
                theme = Theme.YELLOW;
                break;
            case R.id.radio_light:
                theme = Theme.LIGHT;
                break;
            case R.id.radio_green:
                theme = Theme.GREEN;
                break;
            case R.id.check_sysblack:
                boolean checked = b.checkSysblack.isChecked();
                isBlack(checked);
                Log.i("TAG", "onClick: " + checked);
                if (checked) {
                    SPUtils.getInstance("theme", Context.MODE_PRIVATE).put("theme", Theme.BLACK.getValue());
                    SPUtils.getInstance("theme", Context.MODE_PRIVATE).put("themeName", mStringTheme.get(Theme.BLACK.getValue() - 1));
                    requireActivity().recreate();
                    return;
                }
                break;
            case R.id.check_sys_night:
                theme = Theme.AUTO_BLACK;
                break;

        }
        SPUtils.getInstance("theme", Context.MODE_PRIVATE).put("theme", theme.getValue());
        SPUtils.getInstance("theme", Context.MODE_PRIVATE).put("themeName", mStringTheme.get(theme.getValue() - 1));
        requireActivity().recreate();
    }

    private void isBlack(boolean bool) {
        if (bool) {
            b.radioYellow.setEnabled(false);
            b.radioBlue.setEnabled(false);
            b.radioGreen.setEnabled(false);
            b.radioLight.setEnabled(false);
        } else {
            b.radioYellow.setEnabled(true);
            b.radioBlue.setEnabled(true);
            b.radioGreen.setEnabled(true);
            b.radioLight.setEnabled(true);
        }
    }
}
